<?php

namespace App\Http\Controllers\XhApi;


use App\Models\Xh\QuestionModel;
use App\Repository\Commit\DictConfigRepository;
use App\Repository\XhApi\QuestionRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class KefuController extends BaseController
{
    public $repoist;

    public function index(Request $request)
    {
        $cacheFile = '_xhPageKefuIndex';
        $data = $this->useCache ? Cache::get($cacheFile) : null;
        if (empty($data)) {
            $questionTypeList = [];
            $arr = config('xh.questionType');
            foreach ($arr as $k => $name) {
                $questionTypeList[] = [
                    'index' => $k,
                    'name' => $name,
                    'icon' => '/static/xh/00000' . $k . '.png'
                ];
            }
            $where = [];
            $where['eq']['is_hot'] = 1;
            $RepoistQuestion = new QuestionRepository();
            $fields = ['id', 'title'];
            $orderBy = ['id' => 'desc'];
            $hot = $RepoistQuestion->unifiedGetAll($where, $fields, $orderBy);
            $RepoistDictConfig = new DictConfigRepository();
            $kefuSetting = $RepoistDictConfig->getValue('xhKefuSetting');
            $data = [
                "questionTypeList" => $questionTypeList,
                "adpic" => '/static/xh/answer.png',
                "hot" => $hot,
                "kefu" => $kefuSetting
            ];
//            缓存半小时数据
            Cache::put($cacheFile, $data, 1800);
        }
        return $this->returnMsg(200, '数据已获取', $data);
    }

//列表问题
    public function question(Request $request)
    {
        $type = (int)$request->get('type');
        $cacheFile = '_xhPageKefuQuestion' . $type;
        $data = $this->useCache ? Cache::get($cacheFile) : null;
        if (empty($data)) {
            $questionTypeList = [];
            $arr = config('xh.questionType');
            foreach ($arr as $k => $name) {
                $questionTypeList[] = [
                    'index' => $k,
                    'name' => $name,
                    'icon' => '/static/xh/00000' . $k . '.png'
                ];
            }
            $where = [];
            $where['eq']['type'] = $type;
            $RepoistQuestion = new QuestionRepository();
            $fields = ['id', 'title'];
            $orderBy = ['id' => 'desc'];
            $question = $RepoistQuestion->unifiedGetAll($where, $fields, $orderBy);
            $data = [
                "questionTypeList" => $questionTypeList,
                "question" => $question
            ];
//            缓存半小时数据
            Cache::put($cacheFile, $data, 1800);
        }
        return $this->returnMsg(200, '数据已获取', $data);
    }

//  获取一个问题详情
    public function answer(Request $request)
    {
        $where = [
            'id' => $request->get('id')
        ];
        $RepoistGrid = new QuestionRepository();
        $fields = ['id', 'title', 'answer', 'useful', 'useless'];
        $data = $RepoistGrid->unifiedGetOne($where, $fields);
        if (!empty($data['answer'])) {
            $data['answer'] = rawurlencode(html_entity_decode($data['answer']));
        }
        return $this->returnMsg(200, '数据已获取', $data);
    }

    //  设置有用没用
    public function isUse(Request $request)
    {
        if (empty($request->get('id'))) {
            return $this->returnMsg(2, '请正确操作');
        }
        $where = [
            'id' => intval($request->get('id'))
        ];
        $ModelQuestion = new QuestionModel();
        if ($request->get('use') == 1) {
            $ModelQuestion->where($where)->increment('useful');
        } else {
            $ModelQuestion->where($where)->increment('useless');
        }
        return $this->returnMsg(200, '数据已操作');
    }

//    专属客服页资料
    public function exclusive(Request $request)
    {
        $cacheFile = '_xhPageKefuExclusive';
        $data = $this->useCache ? Cache::get($cacheFile) : null;
        if (empty($data)) {
            $RepoistDictConfig = new DictConfigRepository();
            $kefuSetting = $RepoistDictConfig->getValue('xhKefuSetting');
            $data = [
                "adpic" => '/static/xh/service-head.png',
                "kefu" => $kefuSetting
            ];
//            缓存半小时数据
            Cache::put($cacheFile, $data, 1800);
        }
        return $this->returnMsg(200, '数据已获取', $data);
    }
}
